package org.mics.token.resolver;

import javax.annotation.Resource;
import javax.servlet.http.Cookie;
import javax.servlet.http.HttpServletRequest;

import org.mics.core.model.CurrentUser;
import org.mics.token.annotation.LoginUser;
import org.mics.token.config.TokenConfig;
import org.mics.token.exception.TokenException;
import org.mics.token.service.TokenService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.core.MethodParameter;
import org.springframework.stereotype.Component;
import org.springframework.web.bind.support.WebDataBinderFactory;
import org.springframework.web.context.request.NativeWebRequest;
import org.springframework.web.method.support.HandlerMethodArgumentResolver;
import org.springframework.web.method.support.ModelAndViewContainer;

/**
 * 用户登陆验证
 * @author mics
 * @date 2020年5月22日
 * @version  1.0
 */
@Component
public class LoginUserHandlerMethodArgumentResolver implements HandlerMethodArgumentResolver {
    @Autowired
    private TokenService tokenService;
    @Resource
    private TokenConfig tokenConfig;
    @Override
    public boolean supportsParameter(MethodParameter parameter) {
        return parameter.getParameterType().isAssignableFrom(CurrentUser.class) && parameter.hasParameterAnnotation(LoginUser.class);
    }

    @Override
    public Object resolveArgument(MethodParameter parameter, ModelAndViewContainer container,
                                  NativeWebRequest request, WebDataBinderFactory factory) throws Exception {
        //从header中获取token
        String token = request.getHeader(tokenConfig.getHeader());
        //如果header中不存在token，则从参数中获取token
        if (token == null || token.trim().length() == 0) {
            token = request.getParameter(tokenConfig.getHeader());
        }
        //如果参数中不含有token,则从cookies中取
        if (token == null || token.trim().length() == 0) {
        	Cookie[] cookies = request.getNativeRequest(HttpServletRequest.class).getCookies();
       	 	for(int i =0 ;i< cookies.length;i++) {
       	 		Cookie cookie = cookies[i];
       	 		if (tokenConfig.getHeader().equals(cookie.getName())) {
       	 			token = cookie.getValue();
       	 		}
       	 	}
        }
        if (token == null || token.trim().length() == 0) {
            throw new TokenException("登陆失效,请重新登陆！");
        }
        return tokenService.getUser(token);
    }
}
